//
//  CustomCalloutView.m
//  ikkyuchegjbus
//
//  Created by 石家庄盛航 on 16/9/29.
//  Copyright © 2016年 sjzshtx. All rights reserved.
//

#import "CustomCalloutView.h"


#define kPortraitMargin     10
 #define PortraitMargin     10

#define kTitleWidth         KScreenWidth-60
#define kTitleHeight        20
#define kArrorHeight        20
#define ArrorHeight        10

@interface CustomCalloutView ()
@property (nonatomic, strong) UIImageView *portraitView;
@property (nonatomic, strong) UILabel *subtitleLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *label_time;
@property (nonatomic,strong) UILabel *label_phone;


@end
@implementation CustomCalloutView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
       
    }
    return self;
}
- (void)setModel:(StoreListModel *)model{
    _model = model;
     [self initSubViews];
}

- (void)initSubViews
{
    // 添加图片，即定位小图标的图片
    self.portraitView = [[UIImageView alloc] initWithFrame:CGRectMake(kPortraitMargin-5, 40+PortraitMargin, kArrorHeight, kArrorHeight)];
    [self addSubview:self.portraitView];
    
    // 添加标题，即商户名
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin, PortraitMargin, kTitleWidth, [Utility String:_model.chainName Font:14 Width:KScreenWidth-80])];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.textColor = [UIColor redColor];
    self.titleLabel.text = _model.chainName;
    [self addSubview:self.titleLabel];
    
    UILabel *labelOne = [[UILabel alloc]initWithFrame:CGRectMake(kPortraitMargin, CGRectGetMaxY(self.titleLabel.frame)+10, 30,kTitleHeight )];
    labelOne.font = [UIFont systemFontOfSize:12];
    labelOne.textColor = [UIColor lightGrayColor];
    labelOne.text = @"地址:";
    [self addSubview: labelOne];
    // 添加副标题，即商户地址
    self.subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, CGRectGetMaxY(self.titleLabel.frame)+12, KScreenWidth-110, [Utility String:[NSString stringWithFormat:@"%@ %@",_model.areaInfo,_model.address] Font:12 Width:KScreenWidth-110])];
    self.subtitleLabel.numberOfLines = 0;
    self.subtitleLabel.font = [UIFont systemFontOfSize:12];
    self.subtitleLabel.textColor = [UIColor lightGrayColor];
    self.subtitleLabel.text = [NSString stringWithFormat:@"%@ %@",_model.areaInfo,_model.address];
    [self addSubview:self.subtitleLabel];
    
    UILabel *labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(kPortraitMargin, CGRectGetMaxY(self.subtitleLabel.frame)+10, 30, kTitleHeight)];
    labelTwo.font = [UIFont systemFontOfSize:12];
    labelTwo.textColor = [UIColor lightGrayColor];
    labelTwo.text = @"时间:";
    [self addSubview: labelTwo];
    
    //添加时间
    self.label_time = [[UILabel alloc] initWithFrame:CGRectMake(40, CGRectGetMaxY(self.subtitleLabel.frame)+12, KScreenWidth-110, [Utility String:_model.chainOpeningTime Font:12 Width:KScreenWidth-110])];
    self.label_time.numberOfLines = 0;
    self.label_time.font = [UIFont systemFontOfSize:12];
    self.label_time.textColor = [UIColor lightGrayColor];
    self.label_time.text = _model.chainOpeningTime;
    [self addSubview:self.label_time];
    
    
    UILabel *labelThree = [[UILabel alloc]initWithFrame:CGRectMake(kPortraitMargin, CGRectGetMaxY(self.label_time.frame)+10, 30, kTitleHeight)];
    labelThree.font = [UIFont systemFontOfSize:12];
    labelThree.textColor = [UIColor lightGrayColor];
    labelThree.text = @"电话:";
    [self addSubview: labelThree];
    //添加电话
    self.label_phone = [[UILabel alloc] initWithFrame:CGRectMake(40, CGRectGetMaxY(self.label_time.frame)+12, KScreenWidth-110, [Utility String:_model.chainPhone Font:12 Width:KScreenWidth-110])];
    self.label_phone.numberOfLines = 0;
    self.label_phone.font = [UIFont systemFontOfSize:12];
    self.label_phone.textColor = [UIColor lightGrayColor];
    self.label_phone.text = _model.chainPhone;
    [self addSubview:self.label_phone];
    
}
- (void)jiayou:(UIButton *)btn{
    NSLog(@"quJiaYou");
    [self.delegate quJiaYou];
    
//    NSLog(@"%@=%@=%@", self.subtitleLabel, self.titleLabel,self.distanceLabel);
}
#pragma mark - draw rect

- (void)drawRect:(CGRect)rect{
    [self drawInContext:UIGraphicsGetCurrentContext()];
    self.layer.shadowColor = [[UIColor blackColor] CGColor];
    self.layer.shadowOpacity = 1.0;
    self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
}

- (void)drawInContext:(CGContextRef)context{
    
    CGContextSetLineWidth(context, 2.0);
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    [self getDrawPath:context];
    CGContextFillPath(context);
    
}

- (void)getDrawPath:(CGContextRef)context{
    
    CGRect rrect = self.bounds;
    CGFloat radius = 6.0;
    CGFloat minx = CGRectGetMinX(rrect),
    midx = CGRectGetMidX(rrect),
    maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect),
    maxy = CGRectGetMaxY(rrect)-ArrorHeight;
    
    CGContextMoveToPoint(context, midx+ArrorHeight, maxy);
    CGContextAddLineToPoint(context,midx, maxy+ArrorHeight);
    CGContextAddLineToPoint(context,midx-ArrorHeight, maxy);
    
    CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);
    CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, maxx, radius);
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    CGContextClosePath(context);
}

@end
